package com.yuntongxun.ecdemo.voicechat.ui.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.graphics.Bitmap;

import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.listeners.IRecyclerViewItemListener;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by zhuonan on 2016/11/25.
 */

public class MemberAdapter extends RecyclerView.Adapter {


    private Context mContext;
    private List<UserInfo> notes;
    private IRecyclerViewItemListener mListener;
    Bitmap manBitmap;
    Bitmap womanBitmap;

    public MemberAdapter(Context context, List<UserInfo> noteList, IRecyclerViewItemListener myItemClickListener) {
        mContext = context;
        notes = noteList;
        mListener = myItemClickListener;

        try {
            InputStream manStream = mContext.getAssets().open("ic_man.png");
            InputStream womanStream = mContext.getAssets().open("ic_woman.png");

            manBitmap = BitmapFactory.decodeStream(manStream);
            womanBitmap = BitmapFactory.decodeStream(womanStream);
        }
        catch (IOException e) {
            Log.e("----",e.toString());
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = View.inflate(parent.getContext(), R.layout.adapter_member_item, null);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder=(ViewHolder)holder;

        final String name = notes.get(position).getNickName();
        final String age = notes.get(position).getAge();
        final String nativeLanguages = notes.get(position).getNativeLanguage();
        final String learningLanguages = notes.get(position).getLearningLanguage();

        String languageString = "";
        if (nativeLanguages.trim().length() != 0 && learningLanguages.trim().length() != 0)
        {
            languageString = nativeLanguages + " ➝ " + learningLanguages;
            viewHolder.languagesView.setVisibility(View.VISIBLE);
        }
        else
        {
            viewHolder.languagesView.setVisibility(View.GONE);
        }

        if (notes.get(position).getGender()!=null&&notes.get(position).getGender().equals("男"))
        {
            viewHolder.genderView.setImageBitmap(manBitmap);
        }
        else
        {
            viewHolder.genderView.setImageBitmap(womanBitmap);
        }

        try {
            InputStream is = mContext.getAssets().open("flag/"+notes.get(position).getCountry()+".png");
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            viewHolder.flagView.setImageBitmap(bitmap);
            is.close();
        } catch (IOException e) {
            Log.e("----",e.toString());
        }

        viewHolder.nameView.setVisibility(View.VISIBLE);
        viewHolder.nameView.setText(name);

        viewHolder.ageView.setVisibility(View.VISIBLE);
        viewHolder.ageView.setText(age);

        viewHolder.languagesView.setText(languageString);

       if(notes.get(position).getOnLine()!=null){
           if (notes.get(position).getOnLine().equals(UserInfo.ONLINE)){
               viewHolder.statusImg.setVisibility(View.VISIBLE);
               viewHolder.statusImg.setImageResource(R.drawable.ic_green_bg);
           }else if(notes.get(position).getOnLine().equals(UserInfo.BUSY)){
               viewHolder.statusImg.setVisibility(View.VISIBLE);
               viewHolder.statusImg.setImageResource(R.drawable.ic_busy);
           }else{
               viewHolder.statusImg.setVisibility(View.VISIBLE);
               viewHolder.statusImg.setImageResource(R.drawable.ic_offline);
           }
       }else{
           viewHolder.statusImg.setVisibility(View.VISIBLE);
           viewHolder.statusImg.setImageResource(R.drawable.ic_offline);
       }

    }

    @Override
    public int getItemCount() {
        if (notes!=null){
            return notes.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        @InjectView(R.id.user_status)
        ImageView statusImg;
        @InjectView(R.id.name)
        TextView nameView;
        @InjectView(R.id.gender_img)
        ImageView genderView;
        @InjectView(R.id.age)
        TextView ageView;
        @InjectView(R.id.flag_img)
        ImageView flagView;
        @InjectView(R.id.languages)
        TextView languagesView;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }

        @OnClick(R.id.card_view_layout)
        public void clickItem() {
            if (mListener != null) {
                mListener.onItemClick(getPosition());
            }
        }
    }
}
